<!--系统初始化-->
<template>
    <a-modal v-model="showAddTable" :width="1000"
             title="注册密码卡钥匙             "
             :maskClosable="false"
             :destroyOnClose="true" :footer="null">
        <div style="margin: 0 auto 20px; width: 900px">
            <a-steps class="steps" :current="current" style="margin-bottom: 20px; width: 100%">
                <a-step title="生成密钥分量1" />
                <a-step title="生成密钥分量2" />
                <a-step title="生成密钥分量3" />
                <a-step title="完成" />
            </a-steps>
            <a-form>
                <a-form-item label="请选择UKEY："   v-bind="formStyle">
                    <a-select style="width: 350px" @change="changeLabel"
                              placeholder="全部" v-model="selectUkey.deviceId">
                        <a-select-option v-for="item in dmkNumList" :value="item.value"
                                         :key="item.value">{{item.label }}
                        </a-select-option>
                    </a-select>
                </a-form-item>
                <a-form-item label="请输入PIN码："  v-bind="formStyle" :extra="msg">
                    <a-input placeholder="请输入PIN码" type="password" v-model="selectUkey.pinCode" @change="validePassword(selectUkey.pinCode,'msg')" style="width: 350px"></a-input>
                </a-form-item>
                <a-form-item label=" " v-bind="formStyle" class="noLabelAfter">
                    <a-button type="primary" @click="verifyPinCode">确定</a-button>
                </a-form-item>
            </a-form>
        </div>
    </a-modal>
</template>

<script>
    import verify from 'libs/verify'
    import virtualListApi from '@/api/virtualListApi'
    export default {
        name: 'systemInitKey',
        data () {
            return {
                form: this.$form.createForm (this),
                verify,
                showAddTable: false,
                isEdit:false,
                titleTxt:'',
                hostList:[],
                current:0,
                selectUkey:{
                    label:'',
                    deviceId:undefined,
                    pinCode:''
                },
                scanUKeyTimer:null,
            }
        },
        computed: {
            formStyle () {
                return this.$store.state.common.formStyle
            }
        },
        methods: {
            /**
             * Function    : clearScanTimer
             * Description : 开启扫描UKey设备定时器
             * Input       : None
             * Return      : None
             */
            startScanTimer () {
                let _this = this;
                _this.clearScanTimer ()
                _this.loadSaveCompUKeys ()
                //开启扫描外接密钥存储设备定时器
                _this.scanUKeyTimer = setInterval (function () {
                    _this.loadSaveCompUKeys ()
                }, 1000 * 20)
            },

            /**
             * Function    : clearListTimer
             * Description : 关闭定时器
             * Input       : none
             * Return      : none
             */
            clearScanTimer () {
                let _this = this
                if (_this.scanUKeyTimer !== null) {
                    clearInterval (_this.scanUKeyTimer)
                    _this.scanUKeyTimer = null
                }
            },
            /**
             * Function    : loadSaveCompUKeys
             * Description : UKey加载保存分量UKey
             * Input       : None
             * Return      : None
             */
            loadSaveCompUKeys () {
                let _this = this;
                _this.$axios.get (this.$uurl + '/keyscomm/loadSaveCompUKeys').then (response => {
                    response = response.data;
                    this.dmkNumList = response
                    if(this.dmkNumList.length >0){
                        this.selectUkey.deviceId = this.dmkNumList[0].value
                        this.selectUkey.label = this.dmkNumList[0].label
                    }
                }).catch(e=>{
                    _this.spinning = false
                    _this.$message.error ("请插入Ukey")
                })
            },
            validePassword(value,type){
                this[type] = ''
                if (!value || value.trim ().length === 0) {
                    this[type] = '安全UKey登录密码不能为空'
                } else if (value.trim ().length < 6 || value.trim ().length > 16) {
                    this[type] = '安全UKey登录密码不能为空'
                }
            },
            changeLabel(){
                this.selectUkey.label = this.dmkNumList.filter(item=>item.value=== this.selectUkey.deviceId)[0].label
            },
            /**
             * Function    : verifyPinCode
             * Description : UKeyPIN码验证登录
             * Input       : None
             * Return      : None
             */
            verifyPinCode () {
                let _this = this;
                if(this.selectUkey.pinCode === ''){
                    this.msg = '安全UKey登录密码不能为空'
                    return
                }
                if(this.msg !== ''){
                    return
                }
                let params ={
                    deviceId:this.selectUkey.deviceId,
                    pinCode:Base64.encode(this.selectUkey.pinCode),
                }
                _this.$axios.post (this.$uurl + '/keyscomm/verifyPinCode',params).then (response => {
                    response = response.data;
                    if(response.code === 0){

                    }else{
                        _this.$message.error (response.message)
                    }
                })
            },

            /**
             * Function    : change
             * Description :
             * Input       : val
             * Return      : none
             */
            change ( val ) {
                let _this = this
                setTimeout (function () {
                    if (val) {
                        _this.$refs[ 'refocus' ].focus ()
                    }
                }, 200)
            },
            /**
             * Function    : showModal
             * Description : 打开弹框
             * Input       : none
             * Return      : none
             */
            showModal () {
                this.showAddTable = true
            },

            /**
             * Function    : showEdit
             * Description : 编辑时单位显示
             * Input       : opt单个单位所有数据
             * Return      : none
             */
            showEdit ( opt ) {
                this.id = opt.id
                let _this = this
            },

            /**
             * Function    : saveApply
             * Description : 用户信息新增、编辑保存
             * Input       : name
             * Return      : none
             */
            saveApply ( e ) {
                e.preventDefault ()
                let _this = this
                this.form.validateFields (( err, value ) => {
                    if (!err) {
                        let para = Object.assign ({}, value)
                        let url = virtualListApi.addVirtualApi
                        _this.savePost(url,para)
                    }
                })
            },

            savePost(url,para){
                let _this = this
                para.keyType = '0000'
                _this.$post (url, Object.assign (para)).then (res => {
                    if (res.code === 0) {
                        _this.isFrom = 'detail'
                        _this.$emit ('showDetail',res.data)
                        _this.cancelApply()
                        _this.$message.success (res.message)
                    } else {
                        _this.$message.error (res.message)
                    }
                })
            },

            /**
             * Function    : cancelApply
             * Description : 取消弹出框
             * Input       : name
             * Return      : none
             */
            cancelApply () {
                this.showAddTable = false
                this.isEdit= false
                let _this = this
                setTimeout (function () {
                    _this.form.resetFields ()
                }, 100)
            }
        },

        created () {}
    }
</script>
